package com.hoperun.product.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.honeywell.aidc.BarcodeFailureEvent;
import com.honeywell.aidc.BarcodeReadEvent;
import com.honeywell.aidc.BarcodeReader;
import com.honeywell.aidc.ScannerUnavailableException;
import com.honeywell.aidc.TriggerStateChangeEvent;
import com.honeywell.aidc.UnsupportedPropertyException;
import com.hoperun.distributecloth.R;
import com.hoperun.product.adapter.YdpdAdapter;
import com.hoperun.product.base.BaseActivity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.util.SharedPreUtil;
import com.hoperun.product.view.SimpleDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YDPDActivity extends BaseActivity   {

	private TextView countTv;
	private Button clearBtn, backBtn;
	private ListView listView;
	private EditText clothTktNoEt;

	private YdpdAdapter mAdapter;

	private List<String> mCloths = new ArrayList<String>();
	private com.honeywell.aidc.BarcodeReader barcodeReader;
	// 用户userbean的json数据
	private String mUserBean = "";
	int mCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ydpd_layout);
		initDate();
		initView();
		initEvents();
		//initBarCode();

	}

	private void initDate() {
		mUserBean = (String) SharedPreUtil.getValue(this, SharedPreUtil.USERBEAN, "");

	}

	private void initView() {
		countTv = (TextView) findViewById(R.id.countTv);
		clearBtn = (Button) findViewById(R.id.clearBtn);
		backBtn = (Button) findViewById(R.id.backBtn);
		listView = (ListView) findViewById(R.id.listView);
		clothTktNoEt = (EditText) findViewById(R.id.clothTktNoEt);

		mAdapter = new YdpdAdapter(this, mCloths);
		listView.setAdapter(mAdapter);
		// 设置分割线
		listView.setDivider(getResources().getDrawable(R.color.line));
		// 设置分割线高度
		listView.setDividerHeight(1);

	}

	private void initEvents() {
		clothTktNoEt.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

					startInsertClothTkNo();

					return true;
				}
				return false;
			}
		});

		clearBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCloths.clear();
				mAdapter.notifyDataSetChanged();
				mCount = 0;
				countTv.setText("0");
				clothTktNoEt.setText("");

			}
		});
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				YDPDActivity.this.finish();
			}
		});
	}



	private void startInsertClothTkNo() {
		String clothTkNo = clothTktNoEt.getText().toString().trim();
		if (TextUtils.isEmpty(clothTkNo)) {
			SimpleDialog.forceShow(this, getResources().getString(R.string.please_input_monadno));
			return;
		}
		insertClothTkNo(clothTkNo);
	}

	/**
	 * 插入三联单号
	 * 
	 * @param clothTkNo
	 */
	private void insertClothTkNo(String clothTkNo) {
		showWait();
		RequestServiceImpl.insertYearMothByClothtktNo(mMIPHandler, IServiceImpl.INSERT_MONTH, mUserBean, clothTkNo);
	}

	@Override
	public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
		String msg = "";
		String opt = "";
		if (error) {
			switch (requestType) {
			case IServiceImpl.INSERT_MONTH:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						mCount++;
						countTv.setText(mCount + "");
						String cloth = clothTktNoEt.getText().toString().trim();
						mCloths.add(cloth);
						mAdapter.notifyDataSetChanged();
						clothTktNoEt.setText("");

					} else {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			}

		} else {

			// toast("连接服务器失败！");
			if (objBody != null) {
				String obj = objBody.toString();
				SimpleDialog.show(this, obj);
			} else {
				SimpleDialog.show(this, "链接服务器失败，查询不到错误信息");
			}

		}
		stopWait();
	}



	/************************************霍尼韦尔***************************************/
	private static final String ACTION_BARCODE_DATA = "com.honeywell.sample.action.BARCODE_DATA";
	private static final String ACTION_CLAIM_SCANNER = "com.honeywell.aidc.action.ACTION_CLAIM_SCANNER";
	private static final String ACTION_RELEASE_SCANNER = "com.honeywell.aidc.action.ACTION_RELEASE_SCANNER";
	private static final String EXTRA_SCANNER = "com.honeywell.aidc.extra.EXTRA_SCANNER";
	private static final String EXTRA_PROFILE = "com.honeywell.aidc.extra.EXTRA_PROFILE";
	private static final String EXTRA_PROPERTIES = "com.honeywell.aidc.extra.EXTRA_PROPERTIES";

	private BroadcastReceiver barcodeDataReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			int version = intent.getIntExtra("version", 0);
			if (version >= 1) {
				//ToastUtils.showLong("接收到霍尼韦尔广播数据");
				String data = intent.getStringExtra("data");

				if (!TextUtils.isEmpty(data)) {



					if (!TextUtils.isEmpty(data)) {
						clothTktNoEt.setText(data);
						startInsertClothTkNo();

					}

				}
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(barcodeDataReceiver, new IntentFilter(ACTION_BARCODE_DATA));
		claimScanner();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(barcodeDataReceiver);
		releaseScanner();
	}

	private void claimScanner() {
		Bundle properties = new Bundle();

		properties.putBoolean("DPR_DATA_INTENT", true);
		properties.putString("DPR_DATA_INTENT_ACTION", ACTION_BARCODE_DATA);

		sendBroadcast(new Intent(ACTION_CLAIM_SCANNER).putExtra(EXTRA_SCANNER, "dcs.scanner.imager").putExtra(EXTRA_PROFILE, "MyProfile1").putExtra(EXTRA_PROPERTIES, properties));
	}

	private void releaseScanner() {
		sendBroadcast(new Intent(ACTION_RELEASE_SCANNER));
	}

	/************************************霍尼韦尔***************************************/

}
